/*
 * @Author: xiaoyan
 * @Date: 2021-11-26 16:14:31
 * @LastEditors: xiaoyan
 * @LastEditTime: 2022-01-18 10:40:30
 * @Description: 
 * @FilePath: \user_auth_front\src\store\modules\user.ts
 */
import { getToken } from '@/utils/storage'
const SET_USERINFO = "set_userInfo";
const SET_TOKEN = "set_token";

interface MySate {
    userInfo:Object,
    token:String | undefined,

}
const defaultState:MySate = {
    userInfo:{},
    token:getToken(),
}

type TSetUserInfo = {
    type: typeof SET_USERINFO;
    payload:Object;
}

type TSetToken = {
    type: String;
    payload:String;
}

export type Actions = TSetUserInfo | TSetToken;

export default (state:MySate = defaultState,action:Actions)=>{
    switch (action.type){
        case SET_USERINFO:
            const userInfo = Object.assign({},state.userInfo,{
                ...action.payload
            });
            return Object.assign({},state,{userInfo});
        case SET_TOKEN:
            return Object.assign({},state,{
                token:action.payload
            });
        default:
            return state;
    }
};

export const setUserInfo = (obj:object)=>({
    type:SET_USERINFO,
    payload:obj,
});

export const setToken = (val:string)=>({
    type:SET_TOKEN,
    payload:val,
});
export const removeToken = ()=>({
    type:SET_TOKEN,
    payload:"",
});